import org.joda.time.*


/**
 * Creates event points based for the Operating Date passed in
 * the event points are created and returned in Xml or Json format as necessary
 * The Xml or Json data format is based on the requirements for the Simile Timeline project from MIT
 */
class NodeEventPtsUtil
{
    static EventPt getEventPtFromStrings(String domainName, String name, String startDateStr, String startTimeStr, String endDateStr, String endTimeStr, String isDurationStr, String isExactStr)
    {
        EventPt eventPt = new EventPt()
        eventPt.title = name

        if(startTimeStr!=null && startTimeStr.length()>0)
            eventPt.startDate = Utils.parseDateTimeString(startDateStr + " " + startTimeStr)
        else
            eventPt.startDate = Utils.parseDateTimeString(startDateStr + " " + "00:00:00")

        eventPt.isDuration = Utils.parseBoolean(isDurationStr)

        if(eventPt.isDuration)
        {
            if(endTimeStr!=null && endTimeStr.length()>0)
                eventPt.endDate = Utils.parseDateTimeString(endDateStr + " " + endTimeStr)
            else
                eventPt.endDate = Utils.parseDateTimeString(endDateStr + " " + "00:00:00")
        }

        //eventPt.link = "../"+domainName+"/" +  name.replace(" ", "_") + ".dot.png"//"http://www.google.com/logo.gif"  
        eventPt.link = "../"+domainName+"/" +  name.replace(" ", "_") + ".html"//"http://www.google.com/logo.gif"  
        eventPt.imageLink = "../"+domainName+"/" +  name.replace(" ", "_") + ".dot.png"//"http://www.google.com/logo.gif"  

        return eventPt
    }


    /**
     * get the event points xml
     */
    static def getEventPtsXml(eventPts)
    {
        // now creating the Business process event Pts
        def timelineXml = "<data>"
        eventPts.each {
            timelineXml = timelineXml << it.getXml()
        }

        timelineXml = timelineXml << "</data>"

        return timelineXml
    }


    /**
     * get the event points Json
     */
    static def getEventPtsJson(eventPts)
    {
        def timelineJson = """
        {
          'wiki-url':"http://simile.mit.edu/shelf/", 
          'wiki-section':"BP Timeline", 
          'dateTimeFormat': 'Gregorian',
          'events': [
        """

        eventPts.each {
            timelineJson = timelineJson << it.getJson() << ","
        }

        timelineJson = timelineJson << "]}\n"
        
        def strBuf = new StringBuffer(timelineJson)
        strBuf.setCharAt(strBuf.lastIndexOf(','), ' ' as char)
        
        //logger.info( "JSON:"+strBuf.toString())
        timelineJson = strBuf.toString()

        return timelineJson
    }
}

